﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using TalentPools.Model;

namespace TalentPools.DAL
{
    public class TrainingService
    {
        public bool DeleteTraining1(int resumeId)
        {
            string sql = "delete from training where resumeId=@resumeId";
            SqlParameter para = new SqlParameter("@resumeId", resumeId);
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql, para) > 0;
        }
        public bool DeleteTraining(int trainingId)
        {
            string sql = "delete from training where trainingId=@trainingId";
            SqlParameter para = new SqlParameter("@trainingId", trainingId);
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql, para) == 1;
        }
        public List<training> GetTrain(int resumeId, int trainingId)
        {
            string sql = "select * from training where resumeId=@resumeId and trainingId=@trainingId";
            SqlParameter[] paras = new SqlParameter[] { 
            new SqlParameter("@resumeId",resumeId),
            new SqlParameter("@trainingId",trainingId)
            };
            List<training> list = new List<training>();
            using (SqlDataReader dr=DBHelper.ExecuteGetReader(CommandType.Text,sql,paras))
            {
                while (dr.Read())
                {
                    training train = new training();
                    train.trainingId = Convert.ToInt32(dr["trainingId"]);
                    train.tittle = dr["tittle"].ToString();
                    train.beginTime = Convert.ToDateTime(dr["beginTime"]);
                    train.endTime = Convert.ToDateTime(dr["endTime"]);
                    train.result = dr["result"].ToString();
                    train.details = dr["details"].ToString();
                    list.Add(train);
                }
                return list;
            }
        }
        public List<training> GetTrains(int resumeId)
        {
            string sql = "select * from training where resumeId=@resumeId";
            SqlParameter para = new SqlParameter("@resumeId", resumeId);
            List<training> list = new List<training>();
            using (SqlDataReader dr=DBHelper.ExecuteGetReader(CommandType.Text,sql,para))
            {
                while (dr.Read())
                {
                    training train = new training();
                    train.trainingId = Convert.ToInt32(dr["trainingId"]);
                    train.tittle = dr["tittle"].ToString();
                    train.beginTime = Convert.ToDateTime(dr["beginTime"]);
                    train.endTime = Convert.ToDateTime(dr["endTime"]);
                    train.result = dr["result"].ToString();
                    train.details = dr["details"].ToString();
                    list.Add(train);
                }
                return list;
            }
        }
    }
}
